import QtQuick 2.15
import QtQuick.Controls 2.15
import '../../controls'

Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property var queryData: null
    Component.onCompleted: {
        window.title=qsTr('乐园售货系统')
        timerQuery.start()
    }

    function queryDayPrice()
    {
        //当日交易金额
        var request = HttpHelper.createIMRequest();
        request.onSucceed( function(request){
            loadCostRecord()
            queryData=request.responseData
        } );
        request.onError(  function(request){
            // CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_cost_total")
    }

    function loadCostRecord()
    {
        var request = HttpHelper.createIMRequest();
        // request.enabledLog=true
        request.onSucceed( function(request){
            // console.log('-------water_get_cost_record--------',JSON.stringify(request.responseData))
            lvs.model = request.responseData.list
        } );
        request.onError(  function(request){
        } );
        request.get("/sys/watpc/pc/water_get_cost_record?page=1&limit=20")
    }

    Connections{
        target: window
        function onRefreshData()
        {
            queryDayPrice()
        }
    }


    Timer{

        id:timerQuery
        interval: 10000
        repeat: true
        triggeredOnStart : true
        onTriggered: {
            queryDayPrice()
        }
    }

    Item {
        id:itemTitle
        width: parent.width
        height: 60
        Text {
            color: '#ffffff'
            anchors.verticalCenter: parent.verticalCenter
            x:20
            text: qsTr("最新售货情况")
        }
        Row{
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 20
            spacing: 20
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: {
                    if(queryData && 'cost_total' in queryData)
                    {
                        return  qsTr("今日总金额: ￥")+queryData.cost_total.toFixed(2)+qsTr('元')
                    }
                    return ''
                }
            }
        }
    }
    Item {
        anchors.fill: parent
        anchors.topMargin: itemTitle.height +10
        anchors.bottomMargin: itemBottom.height + 10
        anchors.leftMargin: 20
        anchors.rightMargin: 20
        Rectangle {
            border.color: '#dddddd'
            width: parent.width
            height: parent.height
            Rectangle {
                id:itemHeader
                width: parent.width - 2
                anchors.horizontalCenter: parent.horizontalCenter
                height: 40
                color: '#F5F7FA'
                property var itemWidth: width / repHeader.model.length
                Row{
                    anchors.verticalCenter: parent.verticalCenter
                    height: parent.height
                    width: parent.width
                    Repeater{
                        id:repHeader
                        model: [qsTr('编号'),qsTr('用户头像'),qsTr('用户昵称'),qsTr('性别'),qsTr('订单编号'),qsTr('手环号'),qsTr('消费金额'),qsTr('购买时间'),qsTr('状态')]
                        Text {
                            width: itemHeader.itemWidth
                            height: itemHeader.height
                            color: '#E0000000'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: modelData
                            elide: Text.ElideRight
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }
                }
            }
            ListView{
                id:lvs
                anchors.fill: parent
                anchors.topMargin: itemHeader.height
                model: []
                ScrollBar.vertical: ScrollBar{}
                clip: true
                property var itemWidth: width / repHeader.model.length
                property var itemHeight: 40
                boundsBehavior: Flickable.StopAtBounds
                delegate: Rectangle{
                    width: lvs.width
                    height: lvs.itemHeight
                    property var itemData: lvs.model[index]
                    color: index % 2 === 0 ? '#FFF2F2F2' : '#ffffff'
                    Row{
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.id
                            elide: Text.ElideRight
                        }
                        Item {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            AvatarItem{
                                width: lvs.itemHeight - 10
                                height: lvs.itemHeight - 10
                                source: {
                                    if('avatar' in itemData && itemData.avatar)
                                    {
                                        return itemData.avatar
                                    }
                                    return ''
                                }

                                anchors.centerIn: parent
                            }
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.w_nickname
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.sex === 0 ? '男' : '女'
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.w_no
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.b_name
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.total.toFixed(2)
                            elide: Text.ElideRight
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.create_time
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: {
                                switch(itemData.p_status)
                                {
                                case 0:
                                    return qsTr('待支付')
                                case 1:
                                    return qsTr('已支付')
                                case 2:
                                    return qsTr('已退款')
                                case 3:
                                    return qsTr('已取消')
                                case 10:
                                    return qsTr('未消费')
                                }
                                return  qsTr('未知类型:')+itemData.p_status
                            }
                            elide: Text.ElideRight
                        }
                    }
                }
            }
        }

    }

    Item {
        id: itemBottom
        width: parent.width
        anchors.bottom: parent.bottom
        height: 100
        Row{
            anchors.centerIn: parent
            spacing: 20
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#ffffff'
                    Text {
                        anchors.centerIn: parent
                        color: '#333333'
                        text: qsTr("统计查询")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/cashier/StatisticsQueryCashierView.qml')
                }
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#ffffff'
                    Text {
                        anchors.centerIn: parent
                        color: '#333333'
                        text: qsTr("库存查询")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/cashier/InventoryQueryView.qml')
                }
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#EA9508'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("售货")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/cashier/Sales/SalesProductView.qml')
                }
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#319B0B'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("退货")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/cashier/Sales/SalesProductView.qml',{returnGoods:true})
                }
            }
        }
    }

}
